import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import '../../model/xhsskd.dart';
import '../../pages/xhss/xhssHelper.dart';
import '../../widgets/someCards.dart';
import '../../widgets/userSelect.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';

class XHSSKDAddPage extends StatefulWidget {
  final int typeId;
  @override
  XHSSKDAddPage(this.typeId);

  @override
  XHSSKDAddPageState createState() => new XHSSKDAddPageState();
}

class XHSSKDAddPageState extends State<XHSSKDAddPage> {
  // final TextEditingController c1=new TextEditingController();
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final String now = Helper.DateTimeNowString();
  late DateTime TZTM;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  upload(BuildContext context) async {
    var x1 = new XHSSKD(
        0,
        widget.typeId,
        XHSSHelper.getName(widget.typeId),
        double.parse(c1.text),
        double.parse(c2.text),
        0,
        Helper.DateTimeToString(TZTM),
        now,
        "");
    await XHSSKD.Add(x1);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("调度令简述"),
      ),
      body: XHSSKDAddBody(context),
    );
  }

  XHSSKDAddBody(BuildContext context) => SingleChildScrollView(
        child: XHSSKDAddFields(context),
      );

  XHSSKDAddFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: <Widget>[
            SomeCard.inCard(
                // margin: EdgeInsets.all(8.0),
                Column(
              children: <Widget>[
                ListTile(
                  title: Text("原开度"),
                  subtitle: Container(
                      child: TextField(
                    maxLines: 1,
                    controller: c1,
                  )),
                ),
                Divider(
                  height: 2.0,
                ),
                ListTile(
                  title: Text("调整后开度"),
                  subtitle: Container(
                      child: TextField(
                    maxLines: 1,
                    controller: c2,
                  )),
                ),
                Divider(
                  height: 2.0,
                ),
                ListTile(
                  title: Text("调整时间"),
                  subtitle: ElevatedButton(
                      onPressed: () {
                        DatePicker.showDateTimePicker(context,
                            // 是否展示顶部操作按钮
                            showTitleActions: true,
                            // change事件

                            // 确定事件
                            onConfirm: (date) {
                          print('confirm $date');
                          setState(() {
                            TZTM = date;
                          });
                        },
                            // 当前时间
                            currentTime: DateTime.now(),
                            // 语言
                            locale: LocaleType.zh);
                      },
                      child: Text(
                        TZTM == null
                            ? '选择操作完成时间'
                            : Helper.DateTimeToString(TZTM),
                        style: TextStyle(color: Colors.blue),
                      )),
                ),
              ],
            )),
            Container(
              margin: EdgeInsets.only(top: 12.0),
            ),
            Card(
              child: (ListTile(
                title: Center(child: Text("保存")),
                onTap: () {
                  upload(context);
                },
              )),
              color: GlobalConfig.cardBackgroundColor,
              margin: const EdgeInsets.all(36.0),
            ),
          ],
        ),
      );
}
